package cn.px.service.repair.impl;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import cn.px.common.bean.AbstractPageResultBean;
import cn.px.common.util.BeanConverterUtil;
import cn.px.dao.bean.MyPage;
import cn.px.dao.dto.repair.RepairProjectDTO;
import cn.px.dao.entity.repair.RepairProject;
import cn.px.dao.mapper.repair.RepairProjectMapper;
import cn.px.dao.vo.repair.RepairProjectVO;
import cn.px.service.base.impl.BaseServiceImpl;
import cn.px.service.repair.RepairProjectRelationService;
import cn.px.service.repair.RepairProjectService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.weekend.Weekend;

import java.util.List;

/**
 * @Description 点检项目 service 实现类
 * @author 品讯科技
 * @date 2024-08
 * @Version Copyright (c) 2020,北京乾元坤和科技有限公司 All rights reserved.
 */
@AllArgsConstructor
@Service("repairProjectService")
public class RepairProjectServiceImpl extends BaseServiceImpl<RepairProjectMapper, RepairProject, RepairProjectDTO> implements RepairProjectService {
    private RepairProjectRelationService relationService;

    @Override
    public int batchDelete(List<String> ids) {
        ids.forEach(id -> {
            cascadingDeleteByKey(id);
        });
        return 1;
    }

    @Override
    public AbstractPageResultBean<RepairProject> selectPageInfo(RepairProjectDTO record, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        AbstractPageResultBean<RepairProject> pageInfo = new MyPage(mapper.selectList(record));
        return pageInfo;
    }

    @Override
    public MyPage<RepairProjectVO> selectPageList(RepairProjectDTO record, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        MyPage<RepairProjectVO> pageInfo = new MyPage(mapper.selectList(record));
        return pageInfo;
    }

    @Override
    public boolean cascadingDeleteByKey(String id) {
        int result = mapper.deleteByPrimaryKey(id);
        return true;
    }

    @Override
    public RepairProjectVO selectDetail(Object id) {
        RepairProject checkProject = mapper.selectByPrimaryKey(id);
        RepairProjectVO vo = new RepairProjectVO();
        BeanConverterUtil.copyObjectProperties(checkProject, vo);
        return vo;
    }

    @Override
    public List<RepairProjectVO> selectRepairProjectListByRepairId(String id) {
        return mapper.selectRepairProjectListByRepairId(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public RepairProject saveModelByDTO(RepairProjectDTO model) {
        // 设置编码
        //Weekend<RepairProject> weekend = new Weekend<>(RepairProject.class);
        //weekend.weekendCriteria().andIsNotNull(RepairProject::getId);
        //Integer count = mapper.selectCountByExample(weekend);
        //model.setNo(IdGeneratorUtils.getCheckStandardNo(++count));
        RepairProject checkProject = super.saveModelByDTO(model);
        return checkProject;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void modModelByDTO(RepairProjectDTO model) {
        super.modModelByDTO(model);
    }
}
